<?php

class Landlord 
{
	const CT_ERROR = "-1";	//错误牌型
	const CT_PASS = "0";	//不出
	const CT_SINGLE = "1";	//单牌
	const CT_PAIR = "2";	//对子
	const CT_THREE = "3";	//三条
	const CT_BOMB = "4";	//炸弹
	const CT_THREE_WITH_ONE = "5";  //3带1
	const CT_THREE_WITH_PAIR = "6"; //3带2
	const CT_FOUR_WITH_TWO = "7";   //4带2
	const CT_FOUR_WITH_PAIRS = "8"; //4带2
	const CT_Straight = "9";		  //顺子
	const CT_Double_Straight = "10";  //连对
	const CT_Aircraft = "11";  //飞机不带
	const CT_Aircraft_Single = "12"; //飞机带单牌
	const CT_Aircraft_Pair = "13"; //飞机带单牌
	const CT_MISSILE = "14"; //王炸


	protected static $instance = null;
	public static function instance()
	{   
	    if(!self::$instance)
	    {
	        self::$instance = new Landlord();
	    }
	    return self::$instance;
	}

	//发牌 全随机规则
	public function dealCard(){
		$player_cards = array();
		$Card = array(	0x0F,0x0E,
						0x42,0x32,0x22,0x12,0x41,0x31,0x21,0x11,
						0x4D,0x3D,0x2D,0x1D,0x4C,0x3C,0x2C,0x1C,0x4B,0x3B,0x2B,0x1B,0x4A,0x3A,0x2A,0x1A,
						0x49,0x39,0x29,0x19,0x48,0x38,0x28,0x18,0x47,0x37,0x27,0x17,0x46,0x36,0x26,0x16,
						0x45,0x35,0x25,0x15,0x44,0x34,0x24,0x14,0x43,0x33,0x23,0x13
					);
        mt_srand();
		$indexs = array_rand($Card, 3);
		$public_cards = array($Card[$indexs[0]], $Card[$indexs[1]], $Card[$indexs[2]]);
		unset($Card[$indexs[0]]);
		unset($Card[$indexs[1]]);
		unset($Card[$indexs[2]]);

		$indexs = array_rand($Card, 17);
		$card1 = array();
		for ($i=0; $i < count($indexs); $i++) { 
			$card1 [] = $Card[$indexs[$i]];
			unset($Card[$indexs[$i]]);
		}

		$indexs = array_rand($Card, 17);
		$card2 = array();
		for ($i=0; $i < count($indexs); $i++) { 
			$card2 [] = $Card[$indexs[$i]];
			unset($Card[$indexs[$i]]);
		}

		$card3 = array_values($Card);
		
		return array($card1, $card2, $card3, $public_cards);
	}

	public function _cVal($card)
	{
		$value = $card % 16;
		if($value == 1 || $value == 2){
			$value +=13;
		} else if($value <= 13){
			
		} else {
			$value += 2;
		}
		return $value;
	}

	public function sortCard($cards){
		usort($cards, function ($a, $b){
			return  $this->_cVal($b) - $this->_cVal($a);
		});
		return $cards;
	}


	//2.获取具体牌的类型 （实现原理就是 通过出来的张数 和相同的牌来组合牌型 看是否满足）  
	//获取类型  
	public function getCardType($cards)  
	{  
		 array_walk($cards, function(&$value){
		 	$value = $this->_cVal($value);
		 });
		 rsort($cards);

		 $card_count = count($cards);
		//简单牌型  
		switch($card_count)  
		{  
			case 0:      //空牌  
			{  
				return array('type'=>self::CT_PASS, 'value'=>0);  
			}  
			case 1: //单牌  
			{  
				return array('type'=>self::CT_SINGLE, 'value'=>$cards[0]);
			}  
			case 2:      //对牌火箭  
			{  
				if(($cards[0]==17)&&($cards[1]==16)){
					return array('type'=>self::CT_MISSILE, 'value'=>$cards[0]);
				}
				if($cards[0]==$cards[1]){
					return array('type'=>self::CT_PAIR, 'value'=>$cards[0]);
				}
				return array('type'=>self::CT_ERROR, 'value'=>0);
			} 
			case 3:   
			{
				if($cards[0]==$cards[2]){	//三条
					return array('type'=>self::CT_THREE, 'value'=>$cards[0]); 
				}
				return array('type'=>self::CT_ERROR, 'value'=>0);
			} 
			case 4:   
			{
				if($cards[0]==$cards[3]){	//炸
					return array('type'=>self::CT_BOMB, 'value'=>$cards[0]); 
				} else if($cards[0]==$cards[2]){ //3带1
					return array('type'=>self::CT_THREE_WITH_ONE, 'value'=>$cards[0]); 
				} else if($cards[1]==$cards[3]){ //3带1
					return array('type'=>self::CT_THREE_WITH_ONE, 'value'=>$cards[1]); 
				} else {
					return array('type'=>self::CT_ERROR, 'value'=>0);
				}
			} 
			case 5:
			{
				$analysis = $this->analysebCard($cards, $card_count);  
				if($analysis['threeCnt']==1 && $analysis['pairCnt']==1){
					return array('type'=>self::CT_THREE_WITH_PAIR, 'value'=>$analysis['threeCard'][0]); 
				}
				break;
			}
			case 6:
			{
				$analysis = $this->analysebCard($cards, $card_count);  
				if($analysis['fourCnt']==1){
					return array('type'=>self::CT_FOUR_WITH_TWO, 'value'=>$analysis['fourCard'][0]); 
				}
				break;
			}
			case 8:
			{
				$analysis = $this->analysebCard($cards, $card_count);  
				if($analysis['fourCnt']==2 || ($analysis['fourCnt']==1&&$analysis['pairCnt']==2)){
					return array('type'=>self::CT_FOUR_WITH_PAIRS, 'value'=>$analysis['fourCard'][0]);
				}
				break;
			}
		}  
		//分析扑克  
		if(!isset($analysis)){
			$analysis = $this->analysebCard($cards, $card_count);  
		}
		
		//顺子
		if(($analysis['singleCnt']>=5)&&($analysis['singleCnt']==$card_count))  
		{  
			$first=$analysis['singleCard'][0];  
			//错误过虑  
			if($first>=15) return array('type'=>self::CT_ERROR, 'value'=>0);  
			
			for($i=1; $i<$analysis['singleCnt']; $i++)  
			{  
				if($first!=($analysis['singleCard'][$i] +  $i)) return array('type'=>self::CT_ERROR, 'value'=>0);  
			}  
			return array('type'=>self::CT_Straight, 'value'=>$first);
		}
		//连对  
		if(($analysis['pairCnt']>=3)&&($analysis['pairCnt']*2==$card_count))  
		{  
			$first = $analysis['pairCard'][0];  
			//错误过虑  
			if($first>=15) return array('type'=>self::CT_ERROR, 'value'=>0);  

			for($i=1; $i<$analysis['pairCnt']; $i++)  
			{  
				if($first!=($analysis['pairCard'][$i] + $i)) return array('type'=>self::CT_ERROR, 'value'=>0);  
			}
			return array('type'=>self::CT_Double_Straight, 'value'=>$first);
		}  

		//飞机
		if($analysis['threeCnt'] + $analysis['fourCnt'] >= 2){ 	//飞机基本条件

			//飞机不带  
			if($card_count % 3 == 0 && $card_count==$analysis['threeCnt']*3)  
			{
				$straight_count = count($this->straightCards($analysis['threeCard']));
				if($straight_count == $analysis['threeCnt']){
					return array('type'=>self::CT_Aircraft, 'value'=>$analysis['threeCard'][0]);
				}
			}

			//飞机带单牌
			if($card_count % 4 == 0 )  
			{
				$power = $card_count / 4 ;
				$arr = array_merge($analysis['threeCard'], $analysis['fourCard']);
				$straight_cards = $this->straightCards($arr);
				$straight_count = count($straight_cards);
				if($straight_count >= $power){
					return array('type'=>self::CT_Aircraft_Single, 'value'=>$straight_cards[0]);
				}
			}

			//飞机带对子
			if($card_count % 5 == 0 )  
			{
				$power = $card_count / 5 ;
				$straight_count = count($this->straightCards($analysis['threeCard']));
				if($straight_count == $power && $analysis['pairCnt'] + $analysis['fourCnt'] * 2 == $power){
					return array('type'=>self::CT_Aircraft_Pair, 'value'=>$analysis['threeCard'][0]);
				}
			}
		}

		return array('type'=>self::CT_ERROR, 'value'=>0);
	}

	//计算能接成顺的牌张数
	public function straightCards($cards)
	{
		$chooseStraight = array();
		$max_count = 0;
		$cards = array_unique($cards);	//删除重复
		rsort($cards);	//降序
		$total = count($cards);
		for ($i=0; $i < $total; $i++) { 
			if($cards[$i] < 15){
				$straightNumber = 1;
				$tmp = array($cards[$i]);
				for ($j=$i+1; $j < $total; $j++) { 
					if($cards[$j-1] - $cards[$j] == 1){
						$tmp[] = $cards[$j];
						$straightNumber++;
					} else {
						break;
					}
				}
				if($straightNumber > $max_count){
					$max_count =  $straightNumber;
					$chooseStraight = $tmp;
				}
				$max_count = $straightNumber > $max_count ? $straightNumber : $max_count;
				$i = $j - 1;
			}
		}
		return $chooseStraight;
	}


	//分析扑克（参数:1将要出牌的数据，出牌的张数,out存放分析的结构体）
	public function analysebCard($cards, $card_count)  
	{  
		//设置结果  
		$analysis  = array(
			'fourCnt' => 0,                      //四张数目  
			'threeCnt' => 0,                      //三张数目  
			'pairCnt' => 0,                    //两张数目  
			'singleCnt'  => 0,                    //单张数目  
			'fourCard'   => array(), //[MAX_COUNT];        //四张克  
			'threeCard'  => array(), //[MAX_COUNT];       //三张扑克  
			'pairCard'   => array(), //[MAX_COUNT];      //两张扑克  
			'singleCard' => array() //[MAX_COUNT];      //单张扑克  
		);
	   
		//点数分析  
		for($i=0; $i<$card_count; $i++)  
		{  
			$sameCnt=1;  
			//搜索同牌  
			for($j=$i+1; $j<$card_count; $j++)  
			{  
				if($cards[$j] != $cards[$i]){
					break;  
				} 
				$sameCnt++;  
			}  

			//设置结果  
			switch($sameCnt)  
			{  
				case 1:               //单张  
				{  
					$index=$analysis['singleCnt']++;  
					$analysis['singleCard'][$index]=$cards[$i];  
					break;  
				}  
				case 2:               //两张  
				{  
					$index=$analysis['pairCnt']++;
					$analysis['pairCard'][$index]=$cards[$i];
					break;  
				}  
				case 3:               //三张  
				{
					$index=$analysis['threeCnt']++;
					$analysis['threeCard'][$index]=$cards[$i];
					break;  
				}  
				case 4:               //四张  
				{
					$index=$analysis['fourCnt']++;
					$analysis['fourCard'][$index]=$cards[$i];
					break;  
				}  
			}  
			//设置索引  
			$i += $sameCnt-1;  
		}  
	    return $analysis;  
	}

	//能否出牌
	public function canThrowOut($firstCard, $nextCard)  
	{
		$firstCount = count($firstCard);
		$nextCount  = count($nextCard);

		if($nextCount == 0){	//pass
			if($firstCount > 0){	//不是先出牌者，可以pass
				return true;
			} else {
				return false;
			}
		}

		//获取类型  
		$firstInfo=$this->getCardType($firstCard);  
		$nextInfo =$this->getCardType($nextCard);  

		$firstType=$firstInfo['type'];  
		$nextType =$nextInfo['type'];  
		$firstValue=$firstInfo['value'];  
		$nextValue =$nextInfo['value'];  

		//类型判断  
		if($nextType==self::CT_ERROR) return false;

		if($firstCount == 0){
			return true;
		}

		if($firstType==self::CT_ERROR){
			echo "上家什么鬼牌型".PHP_EOL;
			return $false;
		} 

		if($nextType==self::CT_MISSILE) return true;  

		//炸弹判断  
		if(($firstType!=self::CT_BOMB)&&($nextType==self::CT_BOMB)) return true;  
		if(($firstType==self::CT_BOMB)&&($nextType!=self::CT_BOMB)) return false;  

		//规则判断  
		if(($firstType!=$nextType)||($firstCount!=$nextCount))return false;  

		return $nextValue>$firstValue;
	}


	public function chooseCard($firstCard, $myCards)  
	{
		$tips = array();
		usort($myCards, function($a, $b){
			return $this->_cVal($b) - $this->_cVal($a);
		});

		$firstCount = count($firstCard);
		$myCount  = count($myCards);
		//获取类型  
		$firstInfo=$this->getCardType($firstCard);
		$firstType=$firstInfo['type'];  
		$firstValue=$firstInfo['value'];

		//主动出牌，提示最小的
		if(count($firstCard) == 0){
			$single = $this->findSingle($myCards, [], 1);
			$tips[0] = $single;
			return $tips;
		}

		//类型判断  
		if($firstType==self::CT_MISSILE) return $tips;
		if($firstType==self::CT_ERROR){
			echo "上家出的什么鬼牌型".PHP_EOL;
			return $tips;
		} 

		$analysis = $this->analysebMyCards($myCards);

		//开始对比  
		switch($firstType)  
		{  
			case self::CT_SINGLE:
			{
				if($analysis['singleCnt'] > 0){
					for ($i=$analysis['singleCnt']-1; $i >= 0; $i--) { 
						if($this->_cVal($analysis['singleCard'][$i]) > $firstValue){
							$tips[] = array($analysis['singleCard'][$i]);
						} else {
							continue;
						}
					}
				} 
				if($analysis['pairCnt'] > 0){
					for ($i=$analysis['pairCnt']-1; $i >= 0; $i--) { 
						if($this->_cVal($analysis['pairCard'][$i*2]) > $firstValue){
							$tips[] = array($analysis['pairCard'][$i*2]);
						} else {
							continue;
						}
					}
				}
				if($analysis['threeCnt'] > 0){
					for ($i=$analysis['threeCnt']-1; $i >= 0; $i--) { 
						if($this->_cVal($analysis['threeCard'][$i*3]) > $firstValue){
							$tips[] = array($analysis['threeCard'][$i*3]);
						} else {
							continue;
						}
					}
				}
				if($analysis['fourCnt'] > 0){
					for ($i=$analysis['fourCnt']-1; $i >= 0; $i--) { 
						if($this->_cVal($analysis['fourCard'][$i*4]) > $firstValue){
							$tips[] = array($analysis['fourCard'][$i*4]);
						} else {
							continue;
						}
					}
				}
				break;
			}
			case self::CT_PAIR:
			{
				if($analysis['pairCnt'] > 0){
					for ($i=$analysis['pairCnt']-1; $i >= 0; $i--) { 
						if($this->_cVal($analysis['pairCard'][$i*2]) > $firstValue){
							$tips[] = array($analysis['pairCard'][$i*2], $analysis['pairCard'][$i*2+1]);
						} else {
							continue;
						}
					}
				}
				if($analysis['threeCnt'] > 0){
					for ($i=$analysis['threeCnt']-1; $i >= 0; $i--) { 
						if($this->_cVal($analysis['threeCard'][$i*3]) > $firstValue){
							$tips[] = array($analysis['threeCard'][$i*3], $analysis['threeCard'][$i*3+1]);
						} else {
							continue;
						}
					}
				}
				if($analysis['fourCnt'] > 0){
					for ($i=$analysis['fourCnt']-1; $i >= 0; $i--) { 
						if($this->_cVal($analysis['fourCard'][$i*4]) > $firstValue){
							$tips[] = array($analysis['fourCard'][$i*4], $analysis['fourCard'][$i*4+1]);
						} else {
							continue;
						}
					}
				} 
				break;
			} 
			case self::CT_THREE:
			case self::CT_THREE_WITH_ONE:
			case self::CT_THREE_WITH_PAIR:
			{
				if($analysis['threeCnt'] > 0){
					for ($i=$analysis['threeCnt']-1; $i >= 0; $i--) { 
						if($this->_cVal($analysis['threeCard'][$i*3]) > $firstValue){
							$tips[] = array($analysis['threeCard'][$i*3], 
											$analysis['threeCard'][$i*3+1], 
											$analysis['threeCard'][$i*3+2]
										);
						} else {
							continue;
						}
					}
				}
				if($analysis['fourCnt'] > 0){
					for ($i=$analysis['fourCnt']-1; $i >= 0; $i--) { 
						if($this->_cVal($analysis['fourCard'][$i*4]) > $firstValue){
							$tips[] = array($analysis['fourCard'][$i*4], 
											$analysis['fourCard'][$i*4+1], 
											$analysis['fourCard'][$i*4+2]
										);
						} else {
							continue;
						}
					}
				}

				if($firstType == self::CT_THREE_WITH_ONE){
					$tips_cnt = count($tips);
					for ($i=0; $i < $tips_cnt; $i++) { 
						$single = $this->findSingle($myCards, $tips[$i], 1);
						if(count($single)==1){
							$tips[$i][] = $single[0];
						} else {
							unset($tips[$i]);
						}
					}
				}
				if($firstType == self::CT_THREE_WITH_PAIR){
					$tips_cnt = count($tips);
					for ($i=0; $i < $tips_cnt; $i++) { 
						$pair = $this->findPair($myCards, $tips[$i], 1);
						if(count($pair)==2){
							$tips[$i][] = $pair[0];
							$tips[$i][] = $pair[1];
						} else {
							unset($tips[$i]);
						}
					}
				}
				$tips = array_values($tips);
				break;
			}

			case self::CT_BOMB:
			{
				break;
			}
			case self::CT_FOUR_WITH_TWO:  
			case self::CT_FOUR_WITH_PAIRS:  
			{  
				if($analysis['fourCnt'] > 0){
					for ($i=$analysis['fourCnt']-1; $i >= 0; $i--) { 
						if($this->_cVal($analysis['fourCard'][$i*4]) > $firstValue){
							$tips[] = array($analysis['fourCard'][$i*4], $analysis['fourCard'][$i*4+1], $analysis['fourCard'][$i*4+2], $analysis['fourCard'][$i*4+3]);
						} else {
							continue;
						}
					}

					if($firstType == self::CT_FOUR_WITH_TWO){
						$tips_cnt = count($tips);
						for ($i=0; $i < $tips_cnt; $i++) { 
							$single = $this->findSingle($myCards, $tips[$i], 2);
							if(count($single)==2){
								$tips[$i][] = $single[0];
								$tips[$i][] = $single[1];
							} else {
								unset($tips[$i]);
							}
						}
					}
					if($firstType == self::CT_FOUR_WITH_PAIRS){
						$tips_cnt = count($tips);
						for ($i=0; $i < $tips_cnt; $i++) { 
							$pair = $this->findPair($myCards, $tips[$i], 2);
							if(count($pair)==4){
								$tips[$i][] = $pair[0];
								$tips[$i][] = $pair[1];
								$tips[$i][] = $pair[2];
								$tips[$i][] = $pair[3];
							} else {
								unset($tips[$i]);
							}
						}
					}
					$tips = array_values($tips);
				}
				break;
			}  

			case self::CT_Aircraft:
			case self::CT_Aircraft_Single:  
			case self::CT_Aircraft_Pair:  
			{
				if($analysis['threeCnt'] >= 2){
					
					$sAnalysis = $this->tripleStraightMyCards($myCards);
					if($sAnalysis['straightCnt'] > 0){
						if($firstType == self::CT_Aircraft){
							$straightNumber = $firstCount/3;
						} else if($firstType == self::CT_Aircraft_Single){
							$straightNumber = $firstCount/4;
						} else {
							$straightNumber = $firstCount/5;
						}

						for ($i = $sAnalysis['straightCnt']-1; $i >=0 ; $i--) { 
							$cnt = count($sAnalysis['straightCard'][$i]);
							if($cnt >= $straightNumber){
								for ($j=$cnt - $straightNumber ; $j >= 0 ; $j-- ) { 

									if($this->_cVal($sAnalysis['straightCard'][$i][$j][0]) > $firstValue){
										 $tmp =   array_slice($sAnalysis['straightCard'][$i], $j, $straightNumber);
										 $tripleStraight = array();
										 foreach ($tmp as $value) {
										 	$tripleStraight[] = $value[0];
										 	$tripleStraight[] = $value[1];
										 	$tripleStraight[] = $value[2];
										 }
										 $tips[] = $tripleStraight;
									} else {
										continue;
									}
								}
							}
						}

						if($firstType == self::CT_Aircraft_Single){
							$tips_cnt = count($tips);
							for ($i=0; $i < $tips_cnt; $i++) { 
								$single = $this->findSingle($myCards, $tips[$i], $straightNumber);
								if(count($single)==$straightNumber){
									for ($j=0; $j < $straightNumber; $j++) { 
										$tips[$i][] = $single[$j];
									}
								} else {
									unset($tips[$i]);
								}
							}
						}
						if($firstType == self::CT_Aircraft_Pair){
							$tips_cnt = count($tips);
							for ($i=0; $i < $tips_cnt; $i++) { 
								$pair = $this->findPair($myCards, $tips[$i], $straightNumber);
								if(count($pair)== $straightNumber*2){
									for ($j=0; $j < $straightNumber; $j++) { 
										$tips[$i][] = $pair[$j*2];
										$tips[$i][] = $pair[$j*2+1];
									}
								} else {
									unset($tips[$i]);
								}
							}
						}
						$tips = array_values($tips);
					}
				} 
				break;
			}  

			case self::CT_Straight:
			{
				$sAnalysis = $this->straightMyCards($myCards);
				if($sAnalysis['straightCnt'] > 0){
					for ($i = $sAnalysis['straightCnt']-1; $i >=0 ; $i--) { 
						$cnt = count($sAnalysis['straightCard'][$i]);
						if($cnt >= $firstCount){
							for ($j=$cnt - $firstCount ; $j >= 0 ; $j-- ) { 

								if($this->_cVal($sAnalysis['straightCard'][$i][$j]) > $firstValue){
									$tips [] =   array_slice($sAnalysis['straightCard'][$i], $j, $firstCount);
								} else {
									continue;
								}
							}
						}
					}
				}
				break;				
			}
			case self::CT_Double_Straight: {

				$sAnalysis = $this->doubleStraightMyCards($myCards);
				if($sAnalysis['straightCnt'] > 0){
					for ($i = $sAnalysis['straightCnt']-1; $i >=0 ; $i--) { 
						$cnt = count($sAnalysis['straightCard'][$i]);
						$straightNumber = $firstCount/2;
						if($cnt >= $straightNumber){
							for ($j=$cnt - $straightNumber ; $j >= 0 ; $j-- ) { 

								if($this->_cVal($sAnalysis['straightCard'][$i][$j][0]) > $firstValue){
									 $tmp =   array_slice($sAnalysis['straightCard'][$i], $j, $straightNumber);
									 $doubleStraight = array();
									 foreach ($tmp as $value) {
									 	$doubleStraight[] = $value[0];
									 	$doubleStraight[] = $value[1];
									 }
									 $tips[] = $doubleStraight;
								} else {
									continue;
								}
							}
						}
					}
				}
				break;			
			}
		}


		if($analysis['fourCnt'] > 0 && $firstType != self::CT_MISSILE){	//炸弹

			for ($i=0; $i < $analysis['fourCnt']; $i++) { 
				if($firstType != self::CT_BOMB || $this->_cVal($analysis['fourCard'][$i*4]) > $firstValue){
					$tips[] = array($analysis['fourCard'][$i*4], 
									$analysis['fourCard'][$i*4+1], 
									$analysis['fourCard'][$i*4+2], 
									$analysis['fourCard'][$i*4+3]
								);
				}
			}
		} 

		if(count($analysis['missile']) > 0){	//王炸
			$tips[] =$analysis['missile'];
		}  

		return $tips;  
	}

	public function findSingle($myCards, $excludeCards=array(), $findCount=1)
	{
		$retArr = array();
		$cards = array();
		foreach ($myCards as $value) {
			if( !in_array($value, $excludeCards)){
				$cards[] = $value;
			}
		}
		$card_count = count($cards);
		if($findCount > $card_count){
			return $retArr;
		}
	    $analysis = $this->analysebMyCards($cards);

	    $arr1 = array_reverse($analysis['singleCard']);
	    $arr2 = array_reverse($analysis['pairCard']);
	    $arr3 = array_reverse($analysis['threeCard']);
	    $arr4 = array_reverse($analysis['fourCard']);

	    $all = array_merge($arr1, $arr2, $arr3, $arr4);
	    return array_slice($all, 0, $findCount) ;
	}

	public function findPair($myCards, $excludeCards=array(), $findCount=1)
	{
		$retArr = array();
		$cards = array();
		foreach ($myCards as $value) {
			if( !in_array($value, $excludeCards)){
				$cards[] = $value;
			}
		}
		$card_count = count($cards);
		if($findCount * 2 > $card_count){
			return $retArr;
		}
	    $analysis = $this->analysebMyCards($cards);

	    $arr2 = array_reverse($analysis['pairCard']);
	    $arr3 = array();
	    for ($i=$analysis['threeCnt']-1; $i >= 0 ; $i--) { 
	    	$arr3[] = $analysis['threeCard'][$i*3];
	    	$arr3[] = $analysis['threeCard'][$i*3+1];
	    }
	    $arr4 = array_reverse($analysis['fourCard']);

	    $all = array_merge($arr2, $arr3, $arr4);
	    if($findCount * 2 > count($all)){
	    	return $retArr;
	    } else {
	    	return array_slice($all, 0, $findCount*2) ;
	    }
	}

	public function analysebMyCards($cards)  
	{ 
		$card_count = count($cards);
		//设置结果  
		$analysis  = array(
			'fourCnt' => 0,                      //四张数目  
			'threeCnt' => 0,                      //三张数目  
			'pairCnt' => 0,                    //两张数目  
			'singleCnt'  => 0,                    //单张数目  
			'fourCard'   => array(), //[MAX_COUNT];        //四张克  
			'threeCard'  => array(), //[MAX_COUNT];       //三张扑克  
			'pairCard'   => array(), //[MAX_COUNT];      //两张扑克  
			'singleCard' => array(), //[MAX_COUNT];      //单张扑克
			'missile' => array() //王炸
		);
	   
		//点数分析  
		for($i=0; $i<$card_count; $i++)  
		{  
			$sameCnt=1;  
			//搜索同牌  
			$currentValue = $this->_cVal($cards[$i]);
			for($j=$i+1; $j<$card_count; $j++)  
			{  
				if($this->_cVal($cards[$j]) != $currentValue){
					break;  
				} 
				$sameCnt++;  
			}  

			//设置结果  
			switch($sameCnt)  
			{  
				case 1:               //单张  
				{  
					$index=$analysis['singleCnt']++;  
					$analysis['singleCard'][$index]=$cards[$i];  
					break;  
				}  
				case 2:               //两张  
				{  
					$index=$analysis['pairCnt']++;
					$analysis['pairCard'][$index*2]=$cards[$i];
					$analysis['pairCard'][$index*2+1]=$cards[$i+1];
					break;  
				}  
				case 3:               //三张  
				{
					$index=$analysis['threeCnt']++;
					$analysis['threeCard'][$index*3]=$cards[$i];
					$analysis['threeCard'][$index*3+1]=$cards[$i+1];
					$analysis['threeCard'][$index*3+2]=$cards[$i+2];
					
					break;  
				}  
				case 4:               //四张  
				{
					$index=$analysis['fourCnt']++;
					$analysis['fourCard'][$index*4]=$cards[$i];
					$analysis['fourCard'][$index*4+1]=$cards[$i+1];
					$analysis['fourCard'][$index*4+2]=$cards[$i+2];
					$analysis['fourCard'][$index*4+3]=$cards[$i+3];
					break;  
				}  
			}  
			
			$i += $sameCnt-1;  
		}
		//王炸 
		if($analysis['singleCnt'] >= 2 && $analysis['singleCard'][0]==0x0F && $analysis['singleCard'][1]==0x0E){
			$analysis['missile'] = array(0x0F,0x0E);
		}
	    return $analysis;  
	}

	public function straightMyCards($myCards)  
	{ 
		$card_count = count($myCards);
		$analysis  = array(
			'straightCnt'  => 0,                    //单张数目  
			'straightCard' => array(), //[MAX_COUNT];      //单张扑克
		);
		if($card_count < 5){
			return $analysis;
		}
		//排除大小王和2，点数相同的牌只保留一张
		$cards = array();
		for($i=0; $i<$card_count; $i++)  
		{  
			$currentValue = $this->_cVal($myCards[$i]);
			if($currentValue >= 15){
				continue;
			}
			$cards[$currentValue] = $myCards[$i];
		}

		$cards = array_values($cards);
		$card_count = count($cards);

		//点数分析  
		for($i=0; $i<$card_count; $i++)  
		{  
			$tmpCnt=1;
			$tmpStraight = array($cards[$i]);
			$currentValue = $this->_cVal($cards[$i]);

			for($j=$i+1; $j<$card_count; $j++)  
			{  
				if($this->_cVal($cards[$j]) == $currentValue - $tmpCnt){
					
					$tmpStraight[] = $cards[$j];
					$tmpCnt++;  
				} else {
					break;
				}
			}

			if($tmpCnt >= 5){
				$index=$analysis['straightCnt']++;  
				$analysis['straightCard'][$index] = $tmpStraight;  
			}
			$i += $tmpCnt-1;
		}
	    return $analysis;  
	}

	public function doubleStraightMyCards($myCards)  
	{ 
		$card_count = count($myCards);
		$analysis  = array(
			'straightCnt'  => 0,                    //单张数目  
			'straightCard' => array(), //[MAX_COUNT];      //单张扑克
		);
		if($card_count < 6){
			return $analysis;
		}
		//排除大小王和2，点数相同的牌只保留一张
		$cards = array();
		for($i=1; $i<$card_count; $i++)  
		{  
			$currentValue = $this->_cVal($myCards[$i]);
			if($currentValue >= 15){
				continue;
			}
			if($currentValue == $this->_cVal($myCards[$i-1])){
				$cards[$currentValue] = array($myCards[$i-1], $myCards[$i]);
			}
			
		}

		$cards = array_values($cards);
		$card_count = count($cards);

		//点数分析  
		for($i=0; $i<$card_count; $i++)  
		{  
			$tmpCnt=1;
			$tmpStraight = array($cards[$i]);
			$currentValue = $this->_cVal($cards[$i][0]);

			for($j=$i+1; $j<$card_count; $j++)  
			{  
				if($this->_cVal($cards[$j][0]) == $currentValue - $tmpCnt){
					$tmpStraight[] = $cards[$j];
					$tmpCnt++;  
				} else {
					break;
				}
			}

			if($tmpCnt >= 3){
				$index=$analysis['straightCnt']++;  
				$analysis['straightCard'][$index] = $tmpStraight;  
			}
			$i += $tmpCnt-1;
		}
	    return $analysis;  
	}

	public function tripleStraightMyCards($myCards)  
	{ 
		$card_count = count($myCards);
		$analysis  = array(
			'straightCnt'  => 0,                    //单张数目  
			'straightCard' => array(), //[MAX_COUNT];      //单张扑克
		);
		if($card_count < 6){
			return $analysis;
		}

		//排除大小王和2，点数相同的牌只保留一张
		$cards = array();
		for($i=2; $i<$card_count; $i++)  
		{  
			$currentValue = $this->_cVal($myCards[$i]);
			if($currentValue >= 15){
				continue;
			}
			if($currentValue == $this->_cVal($myCards[$i-1]) && $currentValue == $this->_cVal($myCards[$i-2])){
				$cards[$currentValue] = array($myCards[$i-2], $myCards[$i-1], $myCards[$i]);
			}
			
		}

		$cards = array_values($cards);
		$card_count = count($cards);

		//点数分析  
		for($i=0; $i<$card_count; $i++)  
		{  
			$tmpCnt=1;
			$tmpStraight = array($cards[$i]);
			$currentValue = $this->_cVal($cards[$i][0]);

			for($j=$i+1; $j<$card_count; $j++)  
			{  
				if($this->_cVal($cards[$j][0]) == $currentValue - $tmpCnt){
					$tmpStraight[] = $cards[$j];
					$tmpCnt++;  
				} else {
					break;
				}
			}

			if($tmpCnt >= 2){
				$index=$analysis['straightCnt']++;  
				$analysis['straightCard'][$index] = $tmpStraight;  
			}
			$i += $tmpCnt-1;
		}
	    return $analysis;  
	}


}



